const key = require('./key')
const db = require('../db')

const JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = key.serectOrKey

module.exports = passport => {
    passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
        const sqlStr = 'select * from users where id = ?'
        db.query(sqlStr, jwt_payload.id, (err, results) => {
            if(err) return done(err, false);
            if(results.length !== 1) return done(null, false);
            return done(null, results[0])
        })
    }));
}